package com.mall.common.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.common.bean.Message;
import com.mall.common.domain.AreaDomain;
import com.mall.common.service.AreaService;
import com.mall.common.util.ResponseUtils;
import com.mall.common.validation.annotation.RequiredFieldValidator;
import com.mall.common.validation.annotation.RequiredStringValidator;
import com.mall.common.validation.annotation.Validations;

@Controller
@RequestMapping("/admin/system/area")
public class AreaController extends BaseController {

	@Resource private AreaService areaService;

	// 添加
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String add(Long parentId, Model model) {
		if (parentId != null) {
			AreaDomain parent = areaService.find(parentId);
			model.addAttribute("parent", parent);
		}
		return "/admin/system/area/input";
	}
	
	// 是否已存在ajax验证
	@RequestMapping(value="/checkName", method=RequestMethod.POST)
	public String checkName(AreaDomain area, Long parentId, String oldValue, HttpServletResponse response) {
		if (areaService.isNameUnique(parentId, oldValue, area.getName())) {
			return ResponseUtils.renderText("true", response);
		} else {
			return ResponseUtils.renderText("false", response);
		}
	}
	
	// 编辑
	@RequestMapping(value="/edit",method=RequestMethod.GET)
	public String edit(Long id, Model model) {
		AreaDomain area = areaService.find(id);
		AreaDomain parent = area.getParent();
		model.addAttribute("area", area);
		model.addAttribute("parent",parent);
		return "/admin/system/area/input";
	}

	// 列表
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String list(Long parentId, Model model) {
		if (parentId != null) {
			AreaDomain parent = areaService.find(parentId);
			List<AreaDomain> areaList = new ArrayList<AreaDomain>(parent.getChildren());
			model.addAttribute("areaList", areaList);
			model.addAttribute("parent", parent);
		} else {
			List<AreaDomain> areaList = areaService.getRootAreaList();
			model.addAttribute("areaList", areaList);
		}
		return "/admin/system/area/list";
	}

	// 删除
	@RequestMapping(value="/delete", method=RequestMethod.GET)
	@ResponseBody
	public Message delete(Long id) {
		areaService.remove(id);
		return successMessage;
	}
	
	// 保存
	@Validations(
		requiredStrings = { 
			@RequiredStringValidator(fieldName = "name", message = "地区名称不允许为空!")
		}
	)
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public String save(AreaDomain area) {
		Long parentId = area.getId();
		String newName = area.getName();
		if (!areaService.isNameUnique(parentId, null, newName)) {
			return "redirect:/admin/system/area/add.jhtml";
		}
		if (parentId != null) {
			AreaDomain parent = areaService.find(parentId);
			area.setParent(parent);
		} else {
			area.setParent(null);
		}
		areaService.persist(area);
		return "redirect:/admin/system/area/list.jhtml";

	}
	
	// 更新
	@Validations(
		requiredFields = {
			@RequiredFieldValidator(fieldName = "id", message = "地区不存在")	
		},
		requiredStrings = {
			@RequiredStringValidator(fieldName = "name", message = "地区名称不能为空!")
		}
	)
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public String update(AreaDomain area) {
		String redirectUrl = "list.jhtml";
		Long parentId = null;
		AreaDomain persistent = areaService.find(area.getId());
		AreaDomain parent = persistent.getParent();
		if (parent != null) {
			parentId = parent.getId();
			redirectUrl += "?parentId=" + parentId;
		}
		if (!areaService.isNameUnique(parentId, persistent.getName(), area.getName())) {
			return "地区名称已存在!";
		}
		areaService.updateByUpdater(area);
		return "redirect:" + redirectUrl;
	}

}